package com.utils.lib.utilsview;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.graphics.drawable.GradientDrawable;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.utils.lib.R;
import com.utils.lib.activity.PrivacyPolicyActivity;

public class UtilsPrivacyPolicy extends LinearLayout {
    public UtilsPrivacyPolicy(Context context) {
        super(context);
        initView(context, null);
    }

    public UtilsPrivacyPolicy(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public UtilsPrivacyPolicy(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private boolean isFirst = true;             //是否第一次进入

    private int color, bgColor, tvColor, tvLayoutColor;          //要设置的颜色
    private String appName;                        //当前app名称
    private boolean isOnClick = false;           //是否点击过 选择框
    private OnUtilsPrivacyPolicyListener listener; //  监听
    private TextView v;                             //《个人信息保护政策》文案 主要设置颜色用
    private TextView next;                          //允许按钮
    private View checkBox;                          //点击选择框
    private View view;                              //当前页面布局
    // 两次点击按钮之间的点击间隔不能少于1000毫秒
    private static final int MIN_CLICK_DELAY_TIME = 1000;
    private static long lastClickTime;

    public static boolean isFastClick() {
        boolean flag = false;
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            flag = true;
        }
        lastClickTime = curClickTime;
        return flag;
    }

    private void initView(final Context context, AttributeSet o) {
        LayoutInflater.from(context).inflate(R.layout.u_privacy_policy_layout, this);
        view = findViewById(R.id.u_ll_layout);
        next = findViewById(R.id.u_tv_next);
        checkBox = findViewById(R.id.u_tv_checkBox);
        v = findViewById(R.id.u_tv_yinshi);
        v.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isFastClick())
                    PrivacyPolicyActivity.starActivity((FragmentActivity) context, appName, color);
            }
        });
        final SharedPreferences sharedPreferences = context.getSharedPreferences("FirstRun", 0);
        Boolean first_run = sharedPreferences.getBoolean("First", true);
        if (!first_run) {
            if (null != listener)
                listener.onPermission();
            view.setVisibility(GONE);
            isFirst = false;
            return;
        }
        appName = "";
        color = context.getResources().getColor(R.color.u_default_color);
        bgColor = context.getResources().getColor(R.color.u_default_color);
        tvColor = context.getResources().getColor(R.color.u_default_color);
        tvLayoutColor = context.getResources().getColor(R.color.u_default_color);
        if (null != o) {
            //从xml的属性中获取到字体颜色与string
            TypedArray ta = context.obtainStyledAttributes(o, R.styleable.UtilsPrivacyPolicy);
            appName = ta.getString(R.styleable.UtilsPrivacyPolicy_appName);
            color = ta.getColor(R.styleable.UtilsPrivacyPolicy_themeColor, context.getResources().getColor(R.color.u_default_color));
            bgColor = ta.getColor(R.styleable.UtilsPrivacyPolicy_bg_color, context.getResources().getColor(R.color.u_default_color));
            tvColor = ta.getColor(R.styleable.UtilsPrivacyPolicy_tv_button, context.getResources().getColor(R.color.u_default_color));
            tvLayoutColor = ta.getColor(R.styleable.UtilsPrivacyPolicy_tv_layout_color, context.getResources().getColor(R.color.u_default_color));
            ta.recycle();
        }
        setAttribute(appName, bgColor, tvColor, color, tvLayoutColor);
        checkBox.setSelected(isOnClick);
        checkBox.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                isOnClick = !isOnClick;
                v.setSelected(isOnClick);
            }
        });
        view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        next.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isOnClick) {
                    sharedPreferences.edit().putBoolean("First", false).apply();
                    if (null != listener) listener.onPermission();
                    view.setVisibility(GONE);
                } else {
                    Toast.makeText(context, "您还未同意个人信息保护政策", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    /**
     * 手动设置颜色
     *
     * @param appName    当前标题
     * @param bgColor    布局背景
     * @param tvColor    《个人信息保护政策》文案颜色
     * @param themeColor 主体色
     */
    public void setAttribute(String appName, int bgColor, int tvColor, int themeColor, int tvLayoutColor) {
        Log.d("---------", "2222");
        this.appName = appName;
        this.bgColor = bgColor;
        this.tvColor = tvColor;
        this.color = themeColor;
        if (null != v)
            v.setTextColor(tvColor);
        if (null != next) {
            GradientDrawable myGrad = (GradientDrawable) next.getBackground();
            myGrad.setColor(tvLayoutColor);
        }
        if (null != view) view.setBackgroundColor(bgColor);
    }

    /**
     * 手动设置颜色
     *
     * @param appName    当前标题
     * @param bgColor    布局背景
     * @param tvColor    《个人信息保护政策》文案颜色
     * @param themeColor 主体色
     */
    public void setAttribute(String appName, int bgColor, int tvColor, int themeColor) {
        this.appName = appName;
        this.bgColor = bgColor;
        this.tvColor = tvColor;
        this.color = themeColor;
        if (null != v)
            v.setTextColor(tvColor);
        if (null != next) {
            GradientDrawable myGrad = (GradientDrawable) next.getBackground();
            myGrad.setColor(color);
        }
        if (null != view) view.setBackgroundColor(bgColor);
        invalidate();
    }

    public OnUtilsPrivacyPolicyListener getOnUtilsPrivacyPolicyListener() {
        return listener;
    }

    /**
     * 设置监听
     */
    public void setOnUtilsPrivacyPolicyListener(OnUtilsPrivacyPolicyListener listener) {
        this.listener = listener;
        if (!isFirst) {
            listener.onPermission();
        }
    }

    /**
     * 允许回调监听
     */
    public interface OnUtilsPrivacyPolicyListener {
        /**
         * 允许进入
         */
        void onPermission();
    }
}
